package com.itc.bi.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.bi.config.LocalFileConfig;
import com.itc.bi.config.MinioAPI;
import com.itc.bi.entity.BigScreenImageEntity;
import com.itc.bi.entity.BigScreenPageEntity;
import com.itc.bi.query.QueryImageQuery;
import com.itc.bi.service.BigScreenImageService;
import com.itc.bi.mapper.BigScreenImageMapper;
import com.itc.bi.utils.ImageUtil;
import com.itc.common.core.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
* @author hecai
* @description 针对表【big_screen_image(上传图片记录表)】的数据库操作Service实现
* @createDate 2024-07-01 21:04:51
*/
@Service
public class BigScreenImageServiceImpl extends ServiceImpl<BigScreenImageMapper, BigScreenImageEntity> implements BigScreenImageService{
    @Autowired
    private  LocalFileConfig localFileConfig;
    @Autowired
    private MinioAPI minioAPI;
    //10MB 图片大小
    private static final long MAX_SIZE = 10*1024*1024;

    /**
     * 上传图片
     * @param multipartFile
     * @return
     */
    public String uploadImageMinio(MultipartFile multipartFile) throws Exception {
        Long size = multipartFile.getSize();
        if(size.longValue()>MAX_SIZE){
            throw new ServiceException("图片文件大小超过10MB,请重新上传");
        }
        BigScreenImageEntity bigScreenImageEntity = new BigScreenImageEntity();
        String[] split = multipartFile.getOriginalFilename().split("\\.");
        String name = IdWorker.getId() +"."+split[split.length-1];
        String fileName = DateUtil.formatDate(DateUtil.date())+ "/"+ name;
        minioAPI.uploadInputStream(minioAPI.getBUCKET(), fileName, multipartFile.getInputStream(), multipartFile.getContentType());
        bigScreenImageEntity.setId(IdWorker.getId());
        bigScreenImageEntity.setName(name);
        bigScreenImageEntity.setPath(fileName);
        this.save(bigScreenImageEntity);
        return minioAPI.getENDPOINT()+"/"+minioAPI.getBUCKET()+"/"+fileName;
    }

    /**
     * 上传图片
     * @param multipartFile
     * @return
     */
    public String uploadImage(MultipartFile multipartFile) throws IOException {
        Long size = multipartFile.getSize();
        if(size.longValue()>MAX_SIZE){
            throw new ServiceException("图片文件大小超过10MB,请重新上传");
        }
        BigScreenImageEntity bigScreenImageEntity = new BigScreenImageEntity();
        ImageUtil imageUtil = new ImageUtil();
        InputStream inputStream = multipartFile.getInputStream();
        String[] split = multipartFile.getOriginalFilename().split("\\.");
        String fileName = multipartFile.getOriginalFilename().replace(split[split.length-1],"");
        String path = "upload/image/"+ IdWorker.getId() +"."+split[split.length-1];
        String uploadPath = imageUtil.upload(inputStream, path, localFileConfig);
        bigScreenImageEntity.setId(IdWorker.getId());
        bigScreenImageEntity.setName(fileName);
        bigScreenImageEntity.setPath(uploadPath);
        this.save(bigScreenImageEntity);
        return localFileConfig.getViewPath(uploadPath);
    }

    /**
     * 分页获取数据
     * @param query
     * @return
     */
    public IPage<BigScreenImageEntity> getPage(QueryImageQuery query) {
        Page<BigScreenImageEntity> page = new Page<>(query.getPageIndex(), query.getPageSize());
        page = super.page(page);
        page.getRecords().forEach(item->{
            item.setPath(localFileConfig.getViewPath(item.getPath()));
        });
        return page;
    }
}




